package problems;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


public class Euler024 extends AbstractEuler {

	@Override
	public Number calculate() {
		List<Integer>chars = new ArrayList<Integer>();
		for (int i = 0; i < 10; i++) {
			chars.add(i);
		}
		
		for (int permutation = 2; permutation <= 1000000; permutation++) {
			permutate:
			for (int k = chars.size() - 2; k >= 0; k--) {
				if (chars.get(k) < chars.get(k+1)) {
					for (int l = chars.size() - 1; k < l; l--) {
						if (chars.get(k) < chars.get(l)) {
							Collections.swap(chars, k, l);
							Collections.reverse(chars.subList(k+1, chars.size()));
							break permutate;
						}
					}
				}
			}
		}

		StringBuffer answer = new StringBuffer();
		for (int i : chars) {
			answer.append(i);
		}
		return new BigInteger(answer.toString());
	}

	@Override
	protected Number getCorrectAnswer() {
		return new BigInteger("2783915460");
	}

}
